home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / tool chest / macapp / ad lib 3.0.1 / install ad lib 3.0.1 / Ad Lib 3.0.1 / MacApp Extensions / UAdLibLink.cpp / UAdLibLink.cpp
Encoding:
Text File  |  1999-07-24  |  14.6 KB  |  576 lines  |  [TEXT/CWIE]

  1. //    UAdLibLink.cp
  2. //    Copyright © 1993-1995 Nick Nallick.  All rights reserved.
  3. //
  4. //
  5. //    Changes:
  6. //
  7. //        •    Add MacApp 3.3 compatibility.
  8. //        •    Add MacApp 3.1 compatibility.
  9. //
  10.  
  11. #ifndef __RESOURCES__
  12. #include <Resources.h>
  13. #endif
  14.  
  15. #ifndef __MacAppCompatibility__
  16. #include "MacAppCompatibility.h"
  17. #endif
  18.  
  19. #if qMacAppVersion != 30 && !defined(__LOWMEM__)
  20. #include "LowMem.h"
  21. #endif
  22.  
  23. #pragma segment AdLibLink
  24.  
  25.  
  26. ///////////////////////////////// TAdLibLink /////////////////////////////////
  27. //
  28. //    A behavior to receive AppleEvents from Ad Lib to update the application's
  29. //    'View' resources.  Add this object to your build and derive your application
  30. //    object from TBehaviorApp.  Finally, include the AdLibLink.r file in your build.
  31. //    This will permit your application to be used with the 'Link Application'
  32. //    function in Ad Lib.
  33. //
  34.  
  35. class TAdLibLink : public TBehavior
  36. {
  37. #if qMacAppVersion > 30
  38.     MA_DECLARE_CLASS;
  39. #endif
  40.  
  41.     public:
  42. #if qMacAppVersion > 33
  43.         TAdLibLink(IDType itsIdentifier = kNoIdentifier_AC);
  44.         virtual ~TAdLibLink();
  45. #else
  46.         VIRTUAL    void            IAdLibLink(IDType itsIdentifier);
  47. #endif
  48.  
  49. #if qMacAppVersion == 30 || qMacAppVersion == 31
  50.         VIRTUAL void            DoAppleCommand(CommandNumber cmd, const AppleEvent& message, const AppleEvent& reply);
  51. #else
  52.         VIRTUAL BOOLEAN        DoScriptCommand(CommandNumber cmd, TAppleEvent* message, TAppleEvent* reply);
  53. #endif
  54.  
  55.     private:
  56.                         void            Add(const AppleEvent& message, AppleEvent& reply);
  57.                         void            Change(const AppleEvent& message, AppleEvent& reply);
  58.                         void            Remove(const AppleEvent& message, AppleEvent& reply);
  59.                         void            Set(const AppleEvent& message, AppleEvent& reply);
  60. };
  61.  
  62. #if qMacAppVersion > 30
  63. MA_DEFINE_CLASS_M1(TAdLibLink, TBehavior);
  64. #endif
  65.  
  66.  
  67. #pragma segment MABehaviorRes
  68.  
  69. const short mPingMsg        = 990;
  70. const short mAddMsg            = 991;
  71. const short mChangeMsg    = 992;
  72. const short mRemoveMsg    = 993;
  73. const short mSetMsg            = 994;
  74. const short mUpdateMsg    = 995;
  75.  
  76. #if qMacAppVersion > 33
  77.  
  78. //    Constructor
  79. //    itsIdentifier:    the behavior's identifier
  80. //
  81. TAdLibLink::TAdLibLink(IDType itsIdentifier) :
  82.     TBehavior(itsIdentifier)
  83. {
  84. }
  85.  
  86. // Added destructor    --    MB-9805-29-1941
  87. TAdLibLink::~TAdLibLink()
  88. {
  89. }
  90.  
  91. #endif
  92.  
  93. #if qMacAppVersion == 30 || qMacAppVersion == 31
  94.  
  95. //    Overrides TBehavior
  96. //    Handle Ad Lib's resource saving AppleEvents.
  97. //
  98. MACAPP_METHOD
  99. void TAdLibLink::DoAppleCommand(CommandNumber cmd, const AppleEvent& message, const AppleEvent& reply)
  100. {
  101.     switch (cmd)
  102.     {
  103.         case mPingMsg:        break;
  104.         case mAddMsg:            Add(message, reply);
  105.                                             break;
  106.         case mChangeMsg:    Change(message, reply);
  107.                                             break;
  108.         case mRemoveMsg:    Remove(message, reply);
  109.                                             break;
  110.         case mSetMsg:            Set(message, reply);
  111.                                             break;
  112.         case mUpdateMsg:    UpdateResFile(gApplicationRefNum);
  113.                                             THROWIFRESERROR();
  114.                                             break;
  115.         default:                    TBehavior::DoAppleCommand(cmd, message, reply);
  116.                                             break;
  117.     }
  118. }
  119.  
  120. #else
  121.  
  122. //    Overrides TBehavior
  123. //    Handle Ad Lib's resource saving AppleEvents.
  124. //
  125. // bpa 6/20/97 - changed fMessage to GetAEDesc
  126.  
  127. inline AEDesc GetAEDesc(TAppleEvent* event) 
  128. #if qMacAppVersion > 33
  129. { return *event; }
  130. #else
  131. { return event->fMessage; }
  132. #endif
  133.  
  134. MACAPP_METHOD
  135. BOOLEAN TAdLibLink::DoScriptCommand(CommandNumber cmd, TAppleEvent* message, TAppleEvent* reply)
  136. {
  137.     switch (cmd)
  138.     {
  139.         case mPingMsg:        break;
  140.         case mAddMsg:
  141.         {                                    // Add temp variables to get rid of compiler warning    --    MB-9907-17-1010
  142.                                             AEDesc msgDesc = GetAEDesc(message);
  143.                                             AEDesc replyDesc = GetAEDesc(reply);
  144.                                             Add(msgDesc, replyDesc);
  145.         }
  146.                                             break;
  147.         case mChangeMsg:
  148.         {                                    // Add temp variables to get rid of compiler warning    --    MB-9907-17-1010
  149.                                             AEDesc msgDesc = GetAEDesc(message);
  150.                                             AEDesc replyDesc = GetAEDesc(reply);
  151.                                             Change(msgDesc, replyDesc);
  152.         }
  153.                                             break;
  154.         case mRemoveMsg:
  155.         {                                    // Add temp variables to get rid of compiler warning    --    MB-9907-17-1010
  156.                                             AEDesc msgDesc = GetAEDesc(message);
  157.                                             AEDesc replyDesc = GetAEDesc(reply);
  158.                                             Remove(msgDesc, replyDesc);
  159.         }
  160.                                             break;
  161.         case mSetMsg:
  162.         {                                    // Add temp variables to get rid of compiler warning    --    MB-9907-17-1010
  163.                                             AEDesc msgDesc = GetAEDesc(message);
  164.                                             AEDesc replyDesc = GetAEDesc(reply);
  165.                                             Set(msgDesc, replyDesc);
  166.         }
  167.                                             break;
  168.         case mUpdateMsg:    UpdateResFile(gApplicationRefNum);
  169.                                             THROWIFRESERROR();
  170.                                             break;
  171.         default:                    return TBehavior::DoScriptCommand(cmd, message, reply);
  172.     }
  173.  
  174.     return TRUE;
  175. }
  176.  
  177. #endif
  178.  
  179.  
  180. #pragma segment MAOpen
  181.  
  182. //    Initialize the behavior.
  183. //
  184. //    itsIdentifier:    the behavior's identifier
  185. //
  186. #if qMacAppVersion <= 33
  187. MACAPP_METHOD
  188. void TAdLibLink::IAdLibLink(IDType itsIdentifier)
  189. {
  190.     IBehavior(itsIdentifier);
  191. }
  192. #endif
  193.  
  194.  
  195. #pragma segment AdLibLink
  196.  
  197. #if qMacAppVersion == 30
  198.  
  199. //    Handle the add resource AppleEvent.
  200. //
  201. //    message:        the AppleEvent
  202. //    reply:            the reply AppleEvent
  203. //
  204. void TAdLibLink::Add(const AppleEvent& message, const AppleEvent& reply)
  205. {
  206.     short id, attributes;
  207.     ResType type;
  208.     DescType actualMsgType;
  209.     Size size;
  210.  
  211.     THROWIFOSERR(AEGetParamPtr(message, 'RTYP', 'RSRC', actualMsgType, (Ptr) &type, sizeof(type), size));
  212.     THROWIFOSERR(AEGetParamPtr(message, 'RID ', typeShortInteger, actualMsgType, (Ptr) &id, sizeof(id), size));
  213.     OSErr error = AEGetParamPtr(message, 'ATTR', typeShortInteger, actualMsgType, (Ptr) &attributes, sizeof(id), size);
  214.     Boolean setAttributes = (error == noErr);
  215.     if (error == errAEDescNotFound)
  216.         error = noErr;
  217.     THROWIFOSERR(error);
  218.     THROWIFOSERR(AESizeOfParam(message, 'DATA', actualMsgType, size));
  219.  
  220.     if (size > 0)
  221.     {
  222.         MAVolatileInit(Handle, data, NULL);
  223.         FailInfo fi;
  224.         TRY(fi)
  225.         {
  226.             data = NewPermHandle(size);
  227.             HLock(data);
  228.             THROWIFOSERR(AEGetParamPtr(message, 'DATA', 'data', actualMsgType, *data, size, size));
  229.             HUnlock(data);
  230.             short curRsrcFile = USERESFILE(gApplicationRefNum);
  231.             AddResource(data, type, id, gEmptyString);
  232.             error = ResError();
  233.             USERESFILE(curRsrcFile);
  234.             THROWIFOSERR(error);
  235.             if (setAttributes)
  236.             {
  237.                 SetResAttrs(data, attributes);
  238.                 THROWIFRESERROR();
  239.                 ChangedResource(data);
  240.                 THROWIFRESERROR();
  241.             }
  242.             fi.Success();
  243.         }
  244.         else
  245.         {
  246.             DISPOSEIFHANDLE(data);
  247.             error = fi.error;
  248.         }
  249.     }
  250.     THROWIFOSERR(AEPutParamPtr(reply, keyErrorNumber, typeShortInteger, (Ptr) &error, sizeof(error)));
  251. }
  252.  
  253.  
  254. //    Handle the changed resource AppleEvent.
  255. //
  256. //    message:        the AppleEvent
  257. //    reply:            the reply AppleEvent
  258. //
  259. void TAdLibLink::Change(const AppleEvent& message, const AppleEvent& reply)
  260. {
  261.     short id;
  262.     ResType type;
  263.     DescType actualMsgType;
  264.     Size size;
  265.     OSErr error;
  266.  
  267.     THROWIFOSERR(AEGetParamPtr(message, 'RTYP', 'RSRC', actualMsgType, (Ptr) &type, sizeof(type), size));
  268.     THROWIFOSERR(AEGetParamPtr(message, 'RID ', typeShortInteger, actualMsgType, (Ptr) &id, sizeof(id), size));
  269.     THROWIFOSERR(AESizeOfParam(message, 'DATA', actualMsgType, size));
  270.  
  271.     short curRsrcFile = USERESFILE(gApplicationRefNum);
  272.     Boolean oldPerm = PermAllocation(TRUE);
  273.     Handle rsrc = Get1Resource(type, id);
  274.     error = ResError();
  275.     PermAllocation(oldPerm);
  276.     USERESFILE(curRsrcFile);
  277.     if (error == noErr)
  278.     {
  279.         FailInfo fi;
  280.         TRY(fi)
  281.         {
  282.             SetHandleSize(rsrc, size);
  283.             FailMemError();
  284.             HLock(rsrc);
  285.             THROWIFOSERR(AEGetParamPtr(message, 'DATA', 'data', actualMsgType, *rsrc, size, size));
  286.             HUnlock(rsrc);
  287.             ChangedResource(rsrc);
  288.             THROWIFRESERROR();
  289.             fi.Success();
  290.         }
  291.         else
  292.             error = fi.error;
  293.     }
  294.     THROWIFOSERR(AEPutParamPtr(reply, keyErrorNumber, typeShortInteger, (Ptr) &error, sizeof(error)));
  295. }
  296.  
  297.  
  298. //    Handle the remove resource AppleEvent.
  299. //
  300. //    message:        the AppleEvent
  301. //    reply:            the reply AppleEvent
  302. //
  303. void TAdLibLink::Remove(const AppleEvent& message, const AppleEvent& reply)
  304. {
  305.     short id;
  306.     ResType type;
  307.     DescType actualMsgType;
  308.     Size size;
  309.     OSErr error;
  310.  
  311.     THROWIFOSERR(AEGetParamPtr(message, 'RTYP', 'RSRC', actualMsgType, (Ptr) &type, sizeof(type), size));
  312.     THROWIFOSERR(AEGetParamPtr(message, 'RID ', typeShortInteger, actualMsgType, (Ptr) &id, sizeof(id), size));
  313.  
  314.     short curRsrcFile = USERESFILE(gApplicationRefNum);
  315.     Boolean curResLoad = LMGetResLoad();
  316.     SetResLoad(FALSE);
  317.     Handle rsrc = Get1Resource(type, id);
  318.     error = ResError();
  319.     SetResLoad(curResLoad);
  320.     if (error == noErr)
  321.     {
  322.         RemoveResource(rsrc);
  323.         error = ResError();
  324.     }
  325.     USERESFILE(curRsrcFile);
  326.     THROWIFOSERR(AEPutParamPtr(reply, keyErrorNumber, typeShortInteger, (Ptr) &error, sizeof(error)));
  327. }
  328.  
  329.  
  330. //    Handle the set resource info AppleEvent.
  331. //
  332. //    message:        the AppleEvent
  333. //    reply:            the reply AppleEvent
  334. //
  335. void TAdLibLink::Set(const AppleEvent& message, const AppleEvent& reply)
  336. {
  337.     short oldID, newID;
  338.     ResType type;
  339.     CStr255 name;
  340.     DescType actualMsgType;
  341.     Size size;
  342.     OSErr error;
  343.  
  344.     THROWIFOSERR(AEGetParamPtr(message, 'RTYP', 'RSRC', actualMsgType, (Ptr) &type, sizeof(type), size));
  345.     THROWIFOSERR(AEGetParamPtr(message, 'RID ', typeShortInteger, actualMsgType, (Ptr) &oldID, sizeof(oldID), size));
  346.     THROWIFOSERR(AEGetParamPtr(message, 'ID  ', typeShortInteger, actualMsgType, (Ptr) &newID, sizeof(newID), size));
  347.     THROWIFOSERR(AEGetParamPtr(message, 'NAME', typeChar, actualMsgType, (Ptr) &name[1], sizeof(name) - 1, size));
  348.     name.Length() = (char) size;
  349.  
  350.     short curRsrcFile = USERESFILE(gApplicationRefNum);
  351.     Boolean curResLoad = LMGetResLoad();
  352.     SetResLoad(FALSE);
  353.     Handle rsrc = Get1Resource(type, oldID);
  354.     error = ResError();
  355.     SetResLoad(curResLoad);
  356.     if (error == noErr)
  357.     {
  358.         SetResInfo(rsrc, newID, name);
  359.         error = ResError();
  360.     }
  361.     USERESFILE(curRsrcFile);
  362.     THROWIFOSERR(AEPutParamPtr(reply, keyErrorNumber, typeShortInteger, (Ptr) &error, sizeof(error)));
  363. }
  364.  
  365. #else
  366.  
  367. //    Handle the add resource AppleEvent.
  368. //
  369. //    message:        the AppleEvent
  370. //    reply:            the reply AppleEvent
  371. //
  372. void TAdLibLink::Add(const AppleEvent& message, AppleEvent& reply)
  373. {
  374.     short id, attributes;
  375.     ResType type;
  376.     DescType actualMsgType;
  377.     Size size;
  378.  
  379.     THROWIFOSERR(AEGetParamPtr(&message, 'RTYP', 'RSRC', &actualMsgType, &type, sizeof(type), &size));
  380.     THROWIFOSERR(AEGetParamPtr(&message, 'RID ', typeShortInteger, &actualMsgType, &id, sizeof(id), &size));
  381.     OSErr error = AEGetParamPtr(&message, 'ATTR', typeShortInteger, &actualMsgType, &attributes, sizeof(id), &size);
  382.     Boolean setAttributes = (error == noErr);
  383.     if (error == errAEDescNotFound)
  384.         error = noErr;
  385.     THROWIFOSERR(error);
  386.     THROWIFOSERR(AESizeOfParam(&message, 'DATA', &actualMsgType, &size));
  387.  
  388.     if (size > 0)
  389.     {
  390.         MAVolatileInit(Handle, data, NULL);
  391. #if qMacAppVersion <= 33
  392.         FailInfo fi;
  393.         TRY(fi)
  394. #else
  395.         try
  396. #endif
  397.         {
  398.             data = NewPermHandle(size);
  399.             HLock(data);
  400.             THROWIFOSERR(AEGetParamPtr(&message, 'DATA', 'data', &actualMsgType, *data, size, &size));
  401.             HUnlock(data);
  402.             short curRsrcFile = USERESFILE(gApplicationRefNum);
  403.             AddResource(data, type, id, gEmptyString);
  404.             error = ResError();
  405.             USERESFILE(curRsrcFile);
  406.             THROWIFOSERR(error);
  407.             if (setAttributes)
  408.             {
  409.                 SetResAttrs(data, attributes);
  410.                 THROWIFRESERROR();
  411.                 ChangedResource(data);
  412.                 THROWIFRESERROR();
  413.             }
  414. #if qMacAppVersion <= 33
  415.             fi.Success();
  416. #endif
  417.         }
  418. #if qMacAppVersion <= 33
  419.         else
  420. #else
  421.         catch (CException_AC& theException)
  422. #endif
  423.         {
  424.             DISPOSEIFHANDLE(data);
  425. #if qMacAppVersion <= 33
  426.             error = fi.error;
  427. #else
  428.             error = theException.GetError();
  429. #endif
  430.         }
  431.     }
  432.     THROWIFOSERR(AEPutParamPtr(&reply, keyErrorNumber, typeShortInteger, &error, sizeof(error)));
  433. }
  434.  
  435.  
  436. //    Handle the changed resource AppleEvent.
  437. //
  438. //    message:        the AppleEvent
  439. //    reply:            the reply AppleEvent
  440. //
  441. void TAdLibLink::Change(const AppleEvent& message, AppleEvent& reply)
  442. {
  443.     short id;
  444.     ResType type;
  445.     DescType actualMsgType;
  446.     Size size;
  447.     OSErr error;
  448.  
  449.     THROWIFOSERR(AEGetParamPtr(&message, 'RTYP', 'RSRC', &actualMsgType, &type, sizeof(type), &size));
  450.     THROWIFOSERR(AEGetParamPtr(&message, 'RID ', typeShortInteger, &actualMsgType, &id, sizeof(id), &size));
  451.     THROWIFOSERR(AESizeOfParam(&message, 'DATA', &actualMsgType, &size));
  452.  
  453.     short curRsrcFile = USERESFILE(gApplicationRefNum);
  454.     Boolean oldPerm = PermAllocation(TRUE);
  455.     Handle rsrc = Get1Resource(type, id);
  456.     error = ResError();
  457.     PermAllocation(oldPerm);
  458.     USERESFILE(curRsrcFile);
  459.     if (error == noErr)
  460.     {
  461. #if qMacAppVersion <= 33
  462.         FailInfo fi;
  463.         TRY(fi)
  464. #else
  465.         try
  466. #endif
  467.         {
  468.             SetHandleSize(rsrc, size);
  469.             THROWIFMEMERROR();
  470.             HLock(rsrc);
  471.             THROWIFOSERR(AEGetParamPtr(&message, 'DATA', 'data', &actualMsgType, *rsrc, size, &size));
  472.             HUnlock(rsrc);
  473.             ChangedResource(rsrc);
  474.             THROWIFRESERROR();
  475. #if qMacAppVersion <= 33
  476.             fi.Success();
  477. #endif
  478.         }
  479. #if qMacAppVersion <= 33
  480.         else
  481.             error = fi.error;
  482. #else
  483.         catch (CException_AC& theException)
  484.         {
  485.             error = theException.GetError();
  486.         }
  487. #endif
  488.     }
  489.     THROWIFOSERR(AEPutParamPtr(&reply, keyErrorNumber, typeShortInteger, &error, sizeof(error)));
  490. }
  491.  
  492.  
  493. //    Handle the remove resource AppleEvent.
  494. //
  495. //    message:        the AppleEvent
  496. //    reply:            the reply AppleEvent
  497. //
  498. void TAdLibLink::Remove(const AppleEvent& message, AppleEvent& reply)
  499. {
  500.     short id;
  501.     ResType type;
  502.     DescType actualMsgType;
  503.     Size size;
  504.     OSErr error;
  505.  
  506.     THROWIFOSERR(AEGetParamPtr(&message, 'RTYP', 'RSRC', &actualMsgType, &type, sizeof(type), &size));
  507.     THROWIFOSERR(AEGetParamPtr(&message, 'RID ', typeShortInteger, &actualMsgType, &id, sizeof(id), &size));
  508.  
  509.     short curRsrcFile = USERESFILE(gApplicationRefNum);
  510.     Boolean curResLoad = LMGetResLoad();
  511.     SetResLoad(FALSE);
  512.     Handle rsrc = Get1Resource(type, id);
  513.     error = ResError();
  514.     SetResLoad(curResLoad);
  515.     if (error == noErr)
  516.     {
  517.         RemoveResource(rsrc);
  518.         error = ResError();
  519.     }
  520.     USERESFILE(curRsrcFile);
  521.     THROWIFOSERR(AEPutParamPtr(&reply, keyErrorNumber, typeShortInteger, (Ptr) &error, sizeof(error)));
  522. }
  523.  
  524.  
  525. //    Handle the set resource info AppleEvent.
  526. //
  527. //    message:        the AppleEvent
  528. //    reply:            the reply AppleEvent
  529. //
  530. void TAdLibLink::Set(const AppleEvent& message, AppleEvent& reply)
  531. {
  532.     short oldID, newID;
  533.     ResType type;
  534.     CSTR255 name;
  535.     DescType actualMsgType;
  536.     Size size;
  537.     OSErr error;
  538.  
  539.     THROWIFOSERR(AEGetParamPtr(&message, 'RTYP', 'RSRC', &actualMsgType, &type, sizeof(type), &size));
  540.     THROWIFOSERR(AEGetParamPtr(&message, 'RID ', typeShortInteger, &actualMsgType, &oldID, sizeof(oldID), &size));
  541.     THROWIFOSERR(AEGetParamPtr(&message, 'ID  ', typeShortInteger, &actualMsgType, &newID, sizeof(newID), &size));
  542.     THROWIFOSERR(AEGetParamPtr(&message, 'NAME', typeChar, &actualMsgType, &name[1], sizeof(name) - 1, &size));
  543.     name.Length() = (char) size;
  544.  
  545.     short curRsrcFile = USERESFILE(gApplicationRefNum);
  546.     Boolean curResLoad = LMGetResLoad();
  547.     SetResLoad(FALSE);
  548.     Handle rsrc = Get1Resource(type, oldID);
  549.     error = ResError();
  550.     SetResLoad(curResLoad);
  551.     if (error == noErr)
  552.     {
  553.         SetResInfo(rsrc, newID, name);
  554.         error = ResError();
  555.     }
  556.     USERESFILE(curRsrcFile);
  557.     THROWIFOSERR(AEPutParamPtr(&reply, keyErrorNumber, typeShortInteger, &error, sizeof(error)));
  558. }
  559.  
  560. #endif
  561.  
  562.  
  563. //////////////////////////////////////////////////////////////////////////////
  564.  
  565. #pragma segment AInit
  566.  
  567. void RegisterTAdLibLink();        // function prototype
  568.  
  569. void RegisterTAdLibLink()
  570. {
  571. #if qMacAppVersion == 30
  572.     new TAdLibLink;
  573. #else
  574.     MA_REGISTER_CLASS(TAdLibLink);
  575. #endif
  576. }